# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=OpenSceneGraph
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-git")
pkgver=r14215.2e6f0d7
pkgrel=1
pkgdesc="Open source high performance 3D graphics toolkit (mingw-w64)"
arch=('any')
url="http://www.openscenegraph.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-collada-dom-svn"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-gstreamer"
         "${MINGW_PACKAGE_PREFIX}-gtk2"
         "${MINGW_PACKAGE_PREFIX}-gtkglext"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-SDL"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' '!strip')
source=("${_realname}"::"git+https://github.com/openscenegraph/OpenSceneGraph.git"
        find-collada.patch
        additional-gl-header.patch)
sha256sums=('SKIP'
            '24f64f975e776fc018f9c65a1bcae043dded1d4174342d863f9f205b013acc13'
            '76eafa53bfc4bb9e0260bf1835622e46210b61cf244a0440beceed9969eeaca7')

pkgver() {
  cd "$srcdir/$_realname"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p1 -i ${srcdir}/find-collada.patch
  patch -p1 -i ${srcdir}/additional-gl-header.patch
}

build() {
  export FREETYPE_DIR=${MINGW_PREFIX}

  for builddir in {Release,Debug}-${MINGW_CHOST}; do
    [[ -d ${builddir} ]] && rm -rf ${builddir}
    mkdir -p ${builddir}
    pushd ${builddir}
      [[ "${builddir%-${MINGW_CHOST}}" == "Release" ]] && {
        local _opts="-DBUILD_OSG_EXAMPLES:BOOL=ON"
      } || {
        local _opts="-DBUILD_OSG_EXAMPLES:BOOL=OFF -DBUILD_OSG_APPLICATIONS=OFF"
      }

      msg "Building ${builddir%-${MINGW_CHOST}} version..."

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      COLLADA_DIR=${MINGW_PREFIX} \
      ${MINGW_PREFIX}/bin/cmake.exe \
        -G"MSYS Makefiles" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DCMAKE_BUILD_TYPE=${builddir%-${MINGW_CHOST}} \
        ${_opts} \
        ../${_realname}

      make
    popd
  done
}

package_release() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd Release-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install
}

package_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=$pkgver")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")

  cd Debug-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

package_mingw-w64-i686-OpenSceneGraph-git() {
  package_release
}

package_mingw-w64-i686-OpenSceneGraph-debug-git() {
  package_debug
}

package_mingw-w64-x86_64-OpenSceneGraph-git() {
  package_release
}

package_mingw-w64-x86_64-OpenSceneGraph-debug-git() {
  package_debug
}
